import MapEvent from "./MapEvent"; //地图事件类
import PrimitiveLoader from "./PrimitiveLoader"; //添加场景类
import ModelAndImage from "./ModelAndImage";  //模型和影像
import Draw from "./draw";  //绘制
import Measure from "./Measure"; //测量
import { map3dConfig, center } from "@/const/mapConfig";
import "@/assets/css/index.css";

// 加载cesium的domId
export const CESIUM_ID = "cesiumContainer";

declare global {
  interface Window {
    CesiumNetworkPlug: any;
  }
}

class CesiumInit {
  mapEvent: any; //地图事件类
  primitiveLoader: any; //mars3d功能
  modelAndImage: any; //模型和影像
  map3d: any;
  draw: any;
  measure: any;
  constructor() {
    this.initMap();
  }
  // 初始化地图
  initMap() {
    this.map3d = new window.mars3d.Map(CESIUM_ID, map3dConfig);
    this.map3d.unbindContextMenu(); //解除绑定的右键菜单
    this.map3d.on(window.mars3d.EventType.renderError, () => {
      window.location.reload();
    });
    this.initDataEvent();
  }
  // 初始化数据和事件
  initDataEvent() {
    // 初始化地图事件
    this.mapEvent = new MapEvent(this.map3d);
    // 初始化Primitive数据加载事件
    this.primitiveLoader = new PrimitiveLoader(this.map3d);
    // 初始化模型和影像
    this.modelAndImage = new ModelAndImage(this.map3d);
    // 初始化绘制
    this.draw = new Draw(this.map3d);
    // 初始化测量
    this.measure = new Measure(this.map3d);
    // 初始化缓存控制器
    const OfflineCache = window.CesiumNetworkPlug.OfflineCacheController;
    // 如果需要缓存特定域名的模型数据，可以这样配置：
    OfflineCache.ruleList.add("http://65.65.116.218:8889/");
    OfflineCache.ruleList.add("http://65.65.116.200:8888/");
  }
  // 地球自转
  async loadData() {
    // this.map3d.scene.screenSpaceCameraController.minimumZoomDistance = 50;
    // this.map3d.scene.globe.depthTestAgainstTerrain = true;

    // 初始化罗盘点击事件
    this.map3d.controls.compass.on(window.mars3d.EventType.click, () => {
      this.mapEvent.flyToPoint(center);
    });

    const url = new URL(`../../../public/zzbj.json`, import.meta.url).href;
    const graphicLayer = new window.mars3d.layer.GeoJsonLayer({
      name: "株洲边界",
      url,
    })
    this.map3d.addLayer(graphicLayer)

    // const locationBar = new window.mars3d.control.LocationBar({
    //   template:
    //     "<div>经度:{lng}</div> <div>纬度:{lat}</div> <div class='hide1000'>横{crsx}  纵{crsy}</div> <div>海拔：{alt}米</div> <div class='hide700'>层级：{level}</div><div>方向：{heading}°</div> <div>俯仰角：{pitch}°</div><div class='hide700'>视高：{cameraHeight}米</div><div class='hide700'>帧率：{fps} FPS</div>"
    // })
    // this.map3d.addControl(locationBar);

    await this.mapEvent.flyToPoint({ ...center, duration: 3 });
  }
}

export default CesiumInit;
